// pages/pay/pay.js
Page({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    end_time: '',//结束时间
    clock: '',//显示时间倒计时
    total_micro_second:"",
    items: [
      { src:"/images/weixin.png", name: "微信支付", value: "1",checked:"true" }, 
      { src:"/images/xianjin.jpg", name: "现金支付", value: "2" }
    ],
    pay:"1",
    orderId:"",
    orderNo:"",
    totalMoney:"",
    createTime:""
  },
  onLoad:function(options){
    console.log(options);
    this.setData({
      orderId:options.orderId,
      orderNo:options.orderNo,
      totalMoney: options.totalMoney,
      createTime: options.createTime
    });
    var times = this.data.createTime;
    console.log(times);
    this.setData({
      end_time: new Date(times).getTime() + 900000 //项目截止时间，时间戳，单位毫秒
    })
    console.log('结束时间：' + this.data.end_time);
    //调用上面定义的递归函数，一秒一刷新时间
    countdown(this);
  },
  /**
   * 点击单选按钮设置更新支付方式
   */
  radioChange: function (e) {
    this.setData({
      pay: e.detail.value
    });
  },
  /**
   * 确认支付
   */
  pay_ok: function(){
    var pay_value = this.data.pay;
    var totalMoney = this.data.totalMoney;
    console.log(this.data.orderId);
    // 微信支付
    if(pay_value=="1"){
      wx.request({
        url: 'http://localhost:8080/fullboy/updateOrderState.action',
        data: {
          orderId: this.data.orderId,
          payType:0,
          struts:1
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        method: 'GET',
        success: function (res) {
          wx.reLaunch({
            url: '/pages/expenditure/expenditure?totalMoney=' + totalMoney,
          })
        }
      })
    }else{
      //现金支付
      wx.request({
        url: 'http://localhost:8080/fullboy/updateOrderState.action',
        data: {
          orderId: this.data.orderId,
          payType: 1,
          struts: 0
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        method: 'GET',
        success: function (res) {
          wx.reLaunch({
            url: '/pages/confirm/confirm',
          })
        }
      })
      
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {

  }
})
function countdown(that) {
  var EndTime = that.data.end_time || [];
  var NowTime = new Date().getTime();
  var total_micro_second = EndTime - NowTime || [];
  // console.log('剩余时间：' + total_micro_second);
  // 渲染倒计时时钟
  that.setData({
    clock: dateformat(total_micro_second),
    total_micro_second: total_micro_second
  });
  if (total_micro_second <= 0) {
    //取消订单
    wx.request({
      url: 'http://localhost:8080/fullboy/updateOrderState.action',
      data: {
        orderId: that.data.orderId,
        struts: 2
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: 'GET',
      success: function (res) {
        
      }
    })
    wx.showModal({
      title: '提示',
      content: '订单已过期，请重新下单！',
      showCancel: false,
      complete: function () {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    })
    return;
  }
  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }
    , 1000)
 }
// 时间格式化输出，如11:03 25:19 每1s都会调用一次
 function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return min + "分钟" + sec + "秒";
 }